var app = getApp();
Page({
  data: {
    onShowRefresh: false, // 执行onShow的时候是否需要刷新
    parent: [
      {relationship: 1,relation: '爸爸', hasData: false, avator: '', name: '', isAdmin: false, isCurrentUser: false},
      {relationship: 2,relation: '妈妈', hasData: false, avator: '', name: '', isAdmin: false, isCurrentUser: false},
    ],
    guardians1:[], // 有数据的
    guardians: [], // 没数据的
    originGuardians: [
      {relationship: 3, relation: '爷爷', hasData: false, avator: '', name: '', isAdmin: false},
      {relationship: 4, relation: '奶奶', hasData: false, avator: '', name: '', isAdmin: false},
      {relationship: 5, relation: '外公', hasData: false, avator: '', name: '', isAdmin: false},
      {relationship: 6, relation: '外婆', hasData: false, avator: '', name: '', isAdmin: false},
      {relationship: 7, relation: '其他', hasData: false, avator: '', name: '', isAdmin: false},
      {relationship: 7, relation: '其他', hasData: false, avator: '', name: '', isAdmin: false},
      {relationship: 7, relation: '其他', hasData: false, avator: '', name: '', isAdmin: false}
    ],
    form: {
      childUserId: ''
    },
    options: ''
  },
  onLoad(options){
    this.setData({
      options
    })
    this.getGuardians()
  },
  onShow(){
    if (this.data.onShowRefresh) {
      this.getGuardians()
      this.setData({
        onShowRefresh: false
      })
    }
  },
  getGuardians() {
    this.data.form.childUserId = this.data.options.childUserId
    var that = this
    app.middleware.requestAjax({
      serAddr: app.serAddrs['getGuardianList'],
      data: this.data.form,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        that.handleGuardians(res.data)
      }
    })
  },
  handleGuardians(list){
    // var relations = ['爸爸','妈妈','爷爷','奶奶','外公','外婆','其他']
    var guardians = JSON.parse(JSON.stringify(this.data.originGuardians))
    var newGuardians = []
    var parent = this.data.parent
    var adminId = ''
    list.forEach(v=>{
      // 确定管理员
      if (!adminId && v.isadmin==1){
        adminId = v.guardianUserId
      }
      // 父母
      if (v.relationship <= 2) {
        parent[v.relationship - 1].childUserId = v.childUserId
        parent[v.relationship - 1].hasData = true
        parent[v.relationship - 1].avator = v.guardianHead
        parent[v.relationship - 1].name = v.guardianName
        parent[v.relationship - 1].isAdmin = v.isadmin
        parent[v.relationship - 1].isCurrentUser = v.guardianUserId == app.userInfo.userId
        parent[v.relationship - 1].guardianUserId = v.guardianUserId
      }
      // 非父母
      else if (v.relationship > 2) {
        var index = guardians.findIndex(value=>{
          return v.relationship == value.relationship
        })
        guardians[index].childUserId = v.childUserId
        guardians[index].hasData = true
        guardians[index].avator = v.guardianHead
        guardians[index].name = v.guardianName
        guardians[index].isAdmin = v.isadmin
        guardians[index].guardianUserId = v.guardianUserId
        guardians[index].isCurrentUser = v.guardianUserId == app.userInfo.userId

        newGuardians.push(guardians.splice(index,1)[0])
      }
    })
    
    this.setData({
      guardians1:newGuardians,
      guardians: [...guardians],
      parent,
      adminId
    })
  },
  // 点击头像
  tapGuardian(e){
    var current = this.data.parent[e.detail]
    wx.navigateTo({
      url: app.xhx_route.babyGuardianDetail+'?guardian='+JSON.stringify(current)+'&adminId='+this.data.adminId
    })
  },
  // 点击横条用户
  tapUser(e){
    var current = this.data.guardians1[e.currentTarget.dataset.index]
    wx.navigateTo({
      url: app.xhx_route.babyGuardianDetail+'?guardian='+JSON.stringify(current)+'&adminId='+this.data.adminId
    })
  },
  // 点击”去邀请“
  inviteGuardian(e){
    wx.navigateTo({
      url: app.xhx_route.babyGuardianInvite+'?relation='+e.detail
    })
  },
  inviteNotParent(e){
    var current = this.data.guardians[e.currentTarget.dataset.index]
    wx.navigateTo({
      url: app.xhx_route.babyGuardianInvite+'?relation='+current.relation
    })
  }
})
